/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package src.convertisseur;

import java.io.IOException;
import java.util.List;
import org.jdom.*;

/**
 *
 * @author krishen
 */
public class Utils {

    private static String KEY = "key";
    private static String DATA = "data";
    

    /**
     * Cree un noeud au format graphml
     * @param keyId
     * @param nameVal
     * @return
     * @throws IOException
     */
    public static Element getNameNode(String keyId,String nameVal)  throws IOException{

        Element name = new Element(DATA);
        Attribute nodename = new Attribute(KEY, keyId);
        name.setAttribute(nodename);
        name.setText(nameVal);
        return name;
    }

    /**
     * Genere les attribut en entete
     * @param i
     * @param tableauKey
     * @param type
     * @return
     */

    public static Element getKeyGraph(int i, String[] tableauKey, String type) {

        Element key = new Element(KEY);
        Attribute keyId = new Attribute("id", tableauKey[i]);
        key.setAttribute(keyId);

        Attribute keyFor = new Attribute("for", type);
        key.setAttribute(keyFor);

        Attribute keyAName = new Attribute("attr.name", tableauKey[i]);
        key.setAttribute(keyAName);

        Attribute keyType = new Attribute("attr.type", "string");
        key.setAttribute(keyType);

        return key;
    }

    /**
     * 
     * @param res
     * @param value
     * @return
     */
    public static boolean checkPresence(String [] res, String value){

        boolean estPresent = false;
        for (int i=0; i< res.length; i++){
            //
            if (res[i].equals(value)){
                //System.out.println("---> "+res[i]+ "="+value);
                    estPresent = true;
                
            }
       }
       return estPresent;

    }
/**
 * Recupere la valeur qui suit (Key Value) : donc renvoie value :)
 * @param res
 * @param value
 * @return
 */
    public static String getValuePosition(String [] res, String value){

        String valueRet = "";

         for (int i=0; i< res.length; i++){
            //
            if (res[i].equals(value) && i != (res.length -1)){
               valueRet  = res[i +1];
               
            }
       }
         return valueRet;

    }

    /**
     * Renvoie la valeur d'un noeud
     * @param valeurRef
     * @param node
     * @return
     */
     public static String getValueNode(String valeurRef, Element node) {
        String label = "";
        List listedata = node.getChildren(DATA);
        Element data;
        for (int i = 0; i < listedata.size(); i++) {
            data = (Element) listedata.get(i);
            if (valeurRef.equalsIgnoreCase(data.getAttributeValue(KEY))) {
                label = data.getValue();
                System.out.println("!!!!!!!!!!!val = " + label);
            }
        }
        return label;
    }



}
